﻿using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPIT.RentHouse.Service
{
    public class RolesService : IRolesService
    {
        public List<RoleDTO> GetList()
        {
            using (var db = new RentHouseEntities())
            {
                var bs = new BaseService<T_Roles>(db);
                var list = bs.GetList(e => true).Select(e => new RoleDTO
                {
                    Id = e.Id,
                    Name = e.Name
                }).ToList(); ;
                return list;
            }
        }

        public List<RoleDTO> GetPageList(int start, int length, string name, ref int rowCount)
        {
            using (var db = new RentHouseEntities())
            {
                var bs = new BaseService<T_Roles>(db);
                var query = PredicateExtensions.True<T_Roles>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.And(e => e.Name.Contains(name));
                }
                var list = bs.GetPagedList(start, length, ref rowCount, query, e => e.Id, false).Select(e => new RoleDTO
                {
                    Id = e.Id,
                    Name = e.Name
                }).ToList(); ;
                return list;
            }
        }

        public AjaxResult AddRole(RoleEditDTO dTO)
        {
            //角色表数据添加
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);
                BaseService<T_Permissions> baseService1 = new BaseService<T_Permissions>(db);
                T_Roles roles = new T_Roles()
                {
                    Name = dTO.Name,
                    CreateDateTime = DateTime.Now,
                    IsDeleted = false
                };
                //角色权限关联表数据的添加
                if (dTO.PermissionIds != null && dTO.PermissionIds.Count > 0)
                {
                    foreach (long perId in dTO.PermissionIds)
                    {
                        T_Permissions permissions = baseService1.Get(a => a.Id == perId);
                        roles.T_Permissions.Add(permissions);
                    }
                }

                long id = baseService.Add(roles);
                if (id > 0)
                {
                    return new AjaxResult(ResultState.Success, "");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "添加角色失败！");
                }

            }


        }

        /// <summary>
        /// 根据id获取角色信息
        /// </summary>
        /// <param name="id">角色id</param>
        /// <returns></returns>
        public RoleEditDTO Get(long id)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);
                T_Roles role = baseService.Get(a => a.Id == id);
                RoleEditDTO dTO = new RoleEditDTO();
                dTO.Id = role.Id;
                dTO.Name = role.Name;
                dTO.PermissionIds = role.T_Permissions.Select(a => a.Id).ToList();
                return dTO;
            }
        }

        public AjaxResult UpdateRole(RoleEditDTO dTO)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Roles> baseService = new BaseService<T_Roles>(db);
                BaseService<T_Permissions> baseService1 = new BaseService<T_Permissions>(db);
                //1，先更新角色信息
                T_Roles role = baseService.Get(a => a.Id == dTO.Id);
                role.Name = dTO.Name;

                //2. 更新角色权限对应关系
                //2.1 先删除原来的权限
                role.T_Permissions.Clear();

                //2.2 再添加提交过来的新的权限
                if (dTO.PermissionIds != null && dTO.PermissionIds.Count > 0)
                {
                    foreach (long perId in dTO.PermissionIds)
                    {
                        T_Permissions permissions = baseService1.Get(a => a.Id == perId);
                        role.T_Permissions.Add(permissions);
                    }

                }
                bool flag = baseService.Update(role);
                if (flag)
                {
                    return new AjaxResult(ResultState.Success, "");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "角色修改失败！");
                }


            }
        }
    }
}
